package com.utils

/**
 * Location:
 * E:\ResourceDeploy.jar!\com\zxfonline\deploy\SystemPropertyUtils.class Java
 * compiler version: 6 (50.0) JD-Core Version: 0.7.1
 */
object SystemPropertyUtils {

    const val PLACEHOLDER_PREFIX = "\${"
    const val PLACEHOLDER_SUFFIX = "}"

    fun resolvePlaceholders(text: String): String {

        val buf = StringBuffer(text)

        var startIndex = buf.indexOf("\${")
        while (startIndex != -1) {
            val endIndex = buf.indexOf("}", startIndex + "\${".length)

            if (endIndex != -1) {
                val placeholder = buf.substring(startIndex + "\${".length, endIndex)

                var nextIndex = endIndex + "}".length
                try {
                    var propVal = System.getProperty(placeholder)
                    if (propVal == null) {
                        propVal = System.getenv(placeholder)
                    }
                    if (propVal != null) {
                        buf.replace(startIndex, endIndex + "}".length, propVal)

                        nextIndex = startIndex + propVal.length
                    } else {
                        System.err.println(
                            "Could not resolve placeholder '" + placeholder + "' in [" + text
                                    + "] as system property: neither system property nor environment variable found"
                        )
                    }
                } catch (ex: Throwable) {
                    System.err.println(
                        "Could not resolve placeholder '" + placeholder + "' in [" + text
                                + "] as system property: " + ex
                    )
                }

                startIndex = buf.indexOf("\${", nextIndex)
            } else {
                startIndex = -1
            }
        }

        return buf.toString()
    }
}